import React, { Component } from 'react';
import store from '../../homeworkstore';
import { addSchoolFn, editSchoolFn } from '../../homeworkstore/actions/actions'
class Modal extends Component {
    state = {
        inp: '',  // 要输入的学 

    }
    render() {
        return (
            <div>
                <input type='text' name='inp' value={this.state.inp} onChange={this.editFn} />
                {
                    this.props.flag ?
                        <button onClick={this.addSureFn}>添加确认</button> :
                        <button onClick={this.editSureFn}>编辑确认</button>
                }
            </div>
        );
    };
    editFn = (e) => {
        this.setState({
            [e.target.name]: e.target.value
        })
    }
    // 添加确认
    addSureFn = () => {
        //实现数据的添加,直接对store仓库中的schoolArr数据进行修改
        store.dispatch(addSchoolFn(this.state.inp))
        // 影藏模态框
        this.props.hideFn()
    };
    componentDidMount() {
        // console.log(this.props.school_name);
        // this.setState({
        //     inp: this.props.school_name
        // })
    };
    backeditFn() {
        this.setState({
            inp: this.props.item.school_name
        })
    };
    // 编辑确认
    editSureFn = () => {
        // 获取要编辑的数据
        // console.log(this.props.item);

        store.dispatch(editSchoolFn({ id: this.props.item.id, school_name: this.state.inp }))
        // 影藏模态框
        this.props.hideFn()
    }
}

export default Modal;
